package com.ydles.goods.controller;


import ccom.ydles.entity.Result;
import ccom.ydles.entity.StatusCode;
import com.ydles.goods.pojo.Brand;
import com.ydles.goods.service.BrandService;
import com.ydles.goods.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin //跨越问题
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/findAll")
    public Result<List<Brand>> findAll(){
        List<Brand> brandList = brandService.findAll();
        Result<List<Brand>> tResult = new Result<>(true, StatusCode.OK,StatusCode.SUCCESS, brandList);
        return tResult;
    }

    @GetMapping("findPage/{page}/{size}")
    public Result<List<Brand>> findPage(@RequestParam("page") Integer page
                            ,@RequestParam("size")Integer size){

        List<Brand> brandList = brandService.findPage(page, size);
        return new Result<>(true,StatusCode.OK,StatusCode.SUCCESS,brandList);

    }


    @PostMapping
    public Result login(@RequestBody Brand brand){
        Boolean login = brandService.login(brand);
        if (login){
            //返回前端map userName token
            Map<String,String> map = new HashMap<>();
            map.put("username",brand.getName());
            String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(),brand.getName(),null);
            map.put("token",jwt);

            return new Result<>(true,StatusCode.OK,StatusCode.SUCCESS,map);
        }else {
            return new Result(false,StatusCode.LOGINERROR,"用户名或密码错误");
        }
    }


}
